package com.neuedu.mblog.dao;

import com.neuedu.mblog.entiry.PostAttribute;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
* 项目：his
* 创建时间：  2021-12-28 15:47:16
* 作者 :金山老师
* 描述 : 数据访问层
*/
public class PostAttributeDao extends BaseDao<PostAttribute> {

    private QueryRunner run = new QueryRunner( dataSource );

    @Override
    public List<PostAttribute> selectList(String sql ) throws Exception {
        throw  new Exception("暂未实现的方法");
    }


    /**
     * 批量查询
     * @param sql
     * @param params
     * @return
     * @throws SQLException
     */
    @Override
    public List<PostAttribute> selectList(String sql, Object ... params ) throws SQLException {

        //每张表不一样的放，解析结果集
        List<PostAttribute> list = run.query(sql, new ResultSetHandler<List<PostAttribute>>() {
            @Override
            public List<PostAttribute> handle(ResultSet rs) throws SQLException {
                List list = new ArrayList();
                while(rs.next()) {
                    PostAttribute postAttribute = new PostAttribute();

                    postAttribute.setId(rs.getInt("id"));
                    postAttribute.setContent(rs.getString("content"));
                    postAttribute.setEditor(rs.getString("editor"));

                    list.add(postAttribute);
                }
                return list;
            }
        },params);

        return list;
    }

    /**
     *
     * @param id 查询主键
     * @return  PostAttribute 根据主键查询的数据
     * @throws SQLException
     */
    @Override
    public PostAttribute selectById(Integer id) throws SQLException {

        //参数列表
        Object[] params = {id};

        String sql = "select id, content, editor from mto_post_attribute where id = ?";
        PostAttribute postAttribute = run.query(sql,new ResultSetHandler<PostAttribute>() {
            @Override
            public PostAttribute handle(ResultSet rs) throws SQLException {
                PostAttribute postAttribute = null;
                if(rs.next()) {
                    postAttribute = new PostAttribute();

                        postAttribute.setId(rs.getInt("id"));
                        postAttribute.setContent(rs.getString("content"));
                        postAttribute.setEditor(rs.getString("editor"));
                }
                return  postAttribute;
            }
        },params);
        return postAttribute;
    }

    /**
    *
    * @param postAttribute 待保存的实体对象
    * @return  int 影响行数
    * @throws SQLException
    */
    @Override
    public int save(PostAttribute postAttribute) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append(" INSERT INTO mto_post_attribute (  ");
        sql.append("  content, editor ");
        sql.append(" )  ");
        sql.append(" VALUES ");
        sql.append("   ( ");
        sql.append("       ?,  ?  ");
        sql.append("   )  ");

        Object[] params = {
            
            postAttribute.getContent(),  
            postAttribute.getEditor()
        };

        return run.update(sql.toString(),params);
    }


    /**
    * 根据主键更新
    * @param  postAttribute
    * @return
    * @throws SQLException
    */
    @Override
    public int update(PostAttribute postAttribute) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append(" UPDATE 				");
        sql.append("   mto_post_attribute           ");
        sql.append(" SET                    ");
            
            sql.append("   content  = ?,  ");
            sql.append("   editor  = ?");

        sql.append(" WHERE id= ?            ");

        Object[] params = {
                
                postAttribute.getContent(), 
                postAttribute.getEditor(), 
            postAttribute.getId()
        };
        return run.update(sql.toString(),params);

    }

    @Override
    public int delete(Integer id) throws SQLException {
        String sql = "delete from mto_post_attribute where id = ?";
        Object[] params = {id};
        return run.update(sql,params);
    }

}
